﻿/*********************************************************************************/
/**    Sims3Pack Readme Generator                                               **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using Sims3PackReadmeGenerator.Properties;

namespace Sims3PackReadmeGenerator.Gui
{
    /// <summary>
    /// form for displaying the options dialog
    /// </summary>
    public partial class OptionsGui : Form
    {
       

        #region fields

        /// <summary>
        /// the saved options
        /// </summary>
        private Options _currentOptions;
        /// <summary>
        /// unsaved changes to the options
        /// </summary>
        private Options _newOptions;

        private bool _isFormLoading         = false;
        private bool _isStoreInfoChanged    = false;

        #endregion

        #region events

        public delegate void OptionsFormEventHandler(object sender, EventArgs e);
        public event OptionsFormEventHandler OnReloadStoreInfoHandler;
        public event OptionsFormEventHandler OnDisableStoreInfoHandler;

        /// <summary>
        /// send event: new store info
        /// </summary>
        protected void OnReloadStoreInfo()
        {
            if (OnReloadStoreInfoHandler != null)
                OnReloadStoreInfoHandler(this, new EventArgs());
        }

        /// <summary>
        /// send event: store info not used
        /// </summary>
        protected void OnDisableStoreInfo()
        {
            if (OnDisableStoreInfoHandler != null)
                OnDisableStoreInfoHandler(this, new EventArgs());
        }

        #endregion

        #region constructor

        /// <summary>
        /// initializes the form and gets current options
        /// initializes new options with current options
        /// </summary>
        /// <param name="options">current options</param>
        public OptionsGui(Options options)
        {
            InitializeComponent();
            _currentOptions = options;
            _newOptions = new Options(options);
        }

        #endregion

        #region methods

        /// <summary>
        /// handles the click event of the use store info checkbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxStoreInfo_CheckedChanged(object sender, EventArgs e)
        {
            _newOptions.IsResolveStoreInfo = true;
            btCommit.Enabled = true;

            if (!_isFormLoading)
            {

                if (checkBoxStoreInfo.Checked)
                {
                    panelStoreInfoPath.Enabled = true;
                    _newOptions.IsResolveStoreInfo = true;
                    _isStoreInfoChanged = true;
                    if (txtStoreInfoPath.Text == "")
                    {
                        lookUpStoreDataPath(true);                       
                    }
                    
                }
                else
                {
                    panelStoreInfoPath.Enabled = false;
                    _newOptions.IsResolveStoreInfo = false;
                }                
                
            }
            else
            {
                //if form is loading
                //_isFormLoading = false;
                btCommit.Enabled = false;
                                
            }
        }

        /// <summary>
        /// handles the click event of the store info path button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btStoreInfoPath_Click(object sender, EventArgs e)
        {

            lookUpStoreDataPath(false);

        }

        /// <summary>
        /// get the path of the store items database from the s3s browser
        /// </summary>
        /// <param name="isCheckBoxClicked"></param>
        private void lookUpStoreDataPath(bool isCheckBoxClicked)
        {
            DialogResult result = openFileDialogStoreData.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtStoreInfoPath.Text = openFileDialogStoreData.FileName;
                _newOptions.StoreInfoPath = openFileDialogStoreData.FileName;
                _isStoreInfoChanged = true;
                btCommit.Enabled = true;                
            }
            else
            {
                //cancel after checkbox was enabled -> disable again
                if (isCheckBoxClicked)
                {
                    panelStoreInfoPath.Enabled = false;
                    checkBoxStoreInfo.Checked = false;
                    _newOptions.IsResolveStoreInfo = false;
                }
            }
        
        }

        /// <summary>
        /// initialize the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Options_Gui_Load(object sender, EventArgs e)
        {
            _isFormLoading = true;
            txtStoreInfoPath.Text = _currentOptions.StoreInfoPath;
          
            if (_currentOptions.IsResolveStoreInfo)
            {
                panelStoreInfoPath.Enabled = true;                
                checkBoxStoreInfo.Checked = true;
            }

            if (Settings.Default.Language == "en")
            {
                radioButtonEnglish.Checked = true;
            }
            else if (Settings.Default.Language == "de")
            {
                radioButtonGerman.Checked = true;
            }
            _isFormLoading = false;
        }        

        /// <summary>
        /// use the current options and save them to user settings
        /// afterwards form is closed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btOk_Click(object sender, EventArgs e)
        {
            saveOptions();
        }        

        /// <summary>
        /// use the current options and save them to user settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCommit_Click(object sender, EventArgs e)
        {
            saveOptions();
            btCommit.Enabled = false;
        }
        
        /// <summary>
        /// save the options
        /// </summary>
        private void saveOptions()
        {
            //if store-options got diasabled and have been enbaled before, reload neccessary, so send signal
            if (!_newOptions.IsResolveStoreInfo && _currentOptions.IsResolveStoreInfo)
            {
                OnDisableStoreInfo();
            }
            
            _currentOptions.IsResolveStoreInfo = _newOptions.IsResolveStoreInfo;
            _currentOptions.StoreInfoPath = _newOptions.StoreInfoPath;
            _currentOptions.save();

            //if storeinfo is changed and used, a reload is neccessary. so send signal
            if (_newOptions.IsResolveStoreInfo && _isStoreInfoChanged)
            {
                OnReloadStoreInfo();
            }


        }

        #region language settings

        private void radioButtonEnglish_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonEnglish.Checked == true)
            {
                Settings.Default.Language = "en";
                Settings.Default.Save();
                askForClose();
            }
            
        }

        private void radioButtonGerman_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonGerman.Checked == true)
            {
                Settings.Default.Language = "de";
                Settings.Default.Save();
                askForClose();
            }
        }

        private void radioButtonWinGui_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonWinGui.Checked == true)
            {
                Settings.Default.Language = "";
                Settings.Default.Save();
                askForClose();
            }
        }
        /// <summary>
        /// asks for closing the program 
        /// </summary>
        private void askForClose()
        {
            if (_isFormLoading)
            {
                return;
            }
            DialogResult result = MessageBox.Show(language.messages.messageOptionsCloseProgram,
                                                    language.messages.messageOptionsCloseProgramTitle,
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (result == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        #endregion

        #endregion

        


    }
}
